<?php
// This will save the class.Config.php file AND save the GlobalBan.cfg file on all servers
define("ROOTDIR", dirname(__FILE__)); // Global Constant of root directory
require_once(ROOTDIR."/config/class.Config.php");

// Get the config object, we want to read the database connection information
$config = new Config();

function selfURL() {
  $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
  $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
  return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}

function strleft($s1, $s2) {
  return substr($s1, 0, strpos($s1, $s2));
}

$url = selfURL();
$url = substr($url, 0, strrpos($url, "/")) . "/";

$logo = $_POST['logo'];

if($logo == "") {
  $logo = "logo.png";
}

$emails = explode("\n", $_POST['emails']);
$emailList = "";

// Generate the list of Ban Manager Emails
for($i=0; $i<count($emails); $i++) {
  if(!empty($emails[$i])) {
    $emailList .= "\"".str_replace(array("\r\n", "\n", "\r"), "", $emails[$i])."\"";
    if($i < count($emails)-1) {
      $emailList .= ", ";
    }
  }
}

// Create config file
$fh = fopen("config/class.Config.php", 'w');

// Generate the php config file
$configData = "<?php
/**
 *  This makes it easier to include configuration variables to other classes
 *  by simply extending the class with the Config class.  Any new variables that
 *  get added MUST have a getter as that will be the only way to retrieve a Config
 *  value if the config value needs to be used outside as it's own object.
 *
 *  Change ALL values below to what you desire for your website.  If you did not
 *  change the gban.sql file, then the database name will be global_ban.  Otherwise
 *  all other variables, espeically those in the database block should be changed
 *  appropriately.
 */

class Config {
  /**
   * Site specific settings
   */
  var $"."bansPerPage = ".$_POST['bansPerPage']."; // Number of bans to display on ban list page for each page (-1 show all)
  var $"."maxPageLinks = ".$_POST['numPageLinks']."; // Number of links to show before and after selected page (IE: set at 2 you would see 1 2 ... 10 11 [12] 13 14 ... 23 24)
  var $"."demoRootDir = \"".$_POST['demoDir']."\"; // Folder to save demos to (folder must be relative to banned dir)
  var $"."demoSizeLimit = \"".$_POST['demoSizeLimit']."\"; // Demo size limit in MB
  var $"."siteName = \"".$_POST['siteName']."\"; // The name of your website
  var $"."siteUrl = \"".$url."\"; // Your clan/server's home page
  var $"."siteLogo = \"".$logo."\"; // Found in images directory; you must save your logo to the images dir!!

  /**
   * SMF integration settings
   * The gban tables MUST be installed in your SMF database ($"."dbName = \"YOUR_SMF_DB\")
   * Full power admins are those with FULL ADMIN rights to the SMF boards
   * If you wish to use SMF integration you MUST install the zip under your Forums directory
   * So you will access the pages by going to Forums/banned
   */
  var $"."enableSmfIntegartion = ".$_POST['smfIntegration'].";  // Whether to enable SMF integartion
  var $"."memberGroup = ".$_POST['smfMemberGroup']."; // The SMF group id that contains all your members
  var $"."adminGroup = ".$_POST['smfAdminGroup']."; // The SMF group id that contains all your admins
  var $"."banManagerGroup = ".$_POST['smfBanManagerGroup']."; // The SMF group id that contains all your ban managers
  var $"."fullPowerGroup = ".$_POST['smfFullPowerGroup']."; // The SMF group id that is allowed full access to the GlobalBan site and admin tools

  /**
   * Ban specific settings
   */
  var $"."banMessage = \"".$_POST['banMessage']."\"; // Message to display to those banned
  var $"."appealSite = \"".$_POST['banAppealSite']."\"; // Site where user's should appeal a ban
  var $"."daysBanPending = ".$_POST['daysBanPending']."; // Number of days to keep someone with a \"pending\" ban off the server (0 to let the person come back after being \"banned\"); this only affects \"members\" who do bans longer than 1 day
  var $"."allowAdminBans = ".$_POST['allowAdminBan']."; // Set to true to allow the banning of admins (Default off - false)
  var $"."teachAdmins = ".$_POST['teachAdmins']."; // Teach admins the !banmenu command
  //var $"."numDemosToBan = -1; // The person uploading a demo needs to have X number of people banned from his demos before future uploads will auto-ban. (-1 is off)

  /**
   * Forum Settings
   * Very simple forum integration (Just adds a link button)
   */
  var $"."enableForumLink = ".$_POST['enableForumLink'].";
  var $"."forumURL = \"".$_POST['forumURL']."\"; // Link to your forums

  /**
   * Database Block
   */
  var $"."dbName = \"".$config->dbName."\"; // Set the Database to access (where all gban tables are located, change if you place your gban tables in a different db)
  var $"."dbUserName = \"".$config->dbUserName."\"; // Set the Database's user name login (recommend a user with only select, insert, update, and delete privs)
  var $"."dbPassword = \"".$config->dbPassword."\"; // Set the Database user's password login
  var $"."dbHostName = \"".$config->dbHostName."\"; // Set the Database's host
  var $"."matchHash = \"".$_POST['hash']."\"; // This must match the has found in the ES script (prevent's people from accessing the page outside)
  var $"."createUserCode = \"".$_POST['createUserCode']."\"; // This code must be entered for someone to create a new basic user
  var $"."createSuperCode = \"".$_POST['createSuperCode']."\"; // This code must be entered for someone to create a FULL power user

  /**
   *  Email address of those who should get notices of when a new ban has been added
   *  or changed.
   */
  var $"."sendEmails = ".$_POST['sendEmailsOnBan']."; // Send an email whenever a ban is added or updated (does not include imports)
  var $"."sendDemoEmails = ".$_POST['sendEmailsDemo']."; // Send an email whenever a new demo is added
  var $"."emailFromHeader = \"".$_POST['senderEmail']."\"; // The from email address
  var $"."banManagerEmails = array(".$emailList."); // Who recieves emails when new bans are added

  function __construct() {
  }

  function Config() {
  }
}
?".">
";

fwrite($fh, $configData);
fclose($fh);
?>

<?php
require_once(ROOTDIR."/include/class.rcon.php");
require_once(ROOTDIR."/include/database/class.ServerQueries.php");

$serverQueries = new ServerQueries();

$servers = $serverQueries->getServers();

// Is admin banning allowed
$adminBanning = 0;
if($_POST['allowAdminBan']) $adminBanning = 1;

// Cycle through each server
foreach($servers as $server) {
  $r = new rcon($server->getIp(),$server->getPort(),$server->getRcon());
  $r->Auth(); // Establish the connection
  $command = "gb_saveConfig \"".$server->getId()."\" \"".$url."\" \"".$_POST['banMessage']."\" \"".$_POST['hash']."\" \"".$_POST['teachAdmins']."\" \"".$_POST['siteName']."\" \"".$adminBanning."\" ";
  $r->sendRconCommand($command);
}
?>

<h5>Configuration has been successfully saved to all servers and the webserver.</h5>